package com.woniuxy.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.his.controller.form.AddPissCheckFrom;
import com.woniuxy.his.entity.CheckDetail;
import com.woniuxy.his.entity.PissCheck;
import com.woniuxy.his.mapper.CheckDetailMapper;
import com.woniuxy.his.mapper.PissCheckMapper;
import com.woniuxy.his.service.IPissCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 尿检表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-28
 */
@Service
public class PissCheckServiceImpl extends ServiceImpl<PissCheckMapper, PissCheck> implements IPissCheckService {

    //用于生成检查结果表单
    @Resource
    private PissCheckMapper pissCheckMapper;

    //用于将检查过的患者的检查表中的状态
    @Resource
    private CheckDetailMapper checkDetailMapper;

    @Override
    public Boolean routineUrineTestformSubmission(AddPissCheckFrom addPissCheckFrom) {
        //数据拷贝
        PissCheck pissCheck = new PissCheck();
        BeanUtils.copyProperties(addPissCheckFrom, pissCheck);

        //执行新增操作
        Integer insertDB = pissCheckMapper.insert(pissCheck);

        if (insertDB != null) {
            //数据库新增成功，修改检查详情表的是否已检查字段
            //获取当前检查详情表的id
            Integer checkDetailId = addPissCheckFrom.getCheckDetailId();

            //将检查详情表的 是否已检查字段修改为 yes
            UpdateWrapper<CheckDetail> wrapper = new UpdateWrapper<>();
            wrapper.set("ischecked", "yes");
            wrapper.eq("check_detail_id", checkDetailId);
            Integer updateDB = checkDetailMapper.update(null, wrapper);
            //如果字段修改成功，检查成功
            if (updateDB != null) {
                return true;
            }
        }
        //失败
        return  false;
    }
}
